package com.lsmgc.cms.service.article;

import com.lsmgc.cms.constant.SysConstant;
import com.lsmgc.cms.dao.article.ArticleDao;
import com.lsmgc.cms.entity.article.Article;
import com.lsmgc.cms.entity.article.ArticleQuery;
import com.lsmgc.cms.util.page.Pagination;
import org.apache.commons.lang3.StringUtils;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ArticleService {
    private ArticleDao articleDao = new ArticleDao();

    public boolean addArticle(Article article) throws SQLException {
        int n = articleDao.addArticle(article);
        if (n > 0) {
            return true;
        }
        return false;
    }

    public Pagination getArticleListPage(ArticleQuery articleQuery) throws SQLException {
        Pagination pagination = new Pagination(articleQuery.getPageNo(), articleQuery.getPageSize(), articleDao.getArticleListPageCount(articleQuery).intValue());
        List<Article> list = articleDao.getArticleListPage(articleQuery);
        if (list == null) {
            list = new ArrayList<>();
        }
        pagination.setList(list);
        return pagination;
    }

    public List<Article> getArticleList(ArticleQuery articleQuery) throws SQLException {
        List<Article> list = articleDao.getArticleList(articleQuery);
        if (list == null) {
            list = new ArrayList<>();
        }
        return list;
    }

    public Article getArticleById(Long id) throws SQLException {
        return articleDao.getArticleById(id);
    }

    public boolean updateArticleById(Article article) throws SQLException {
        int n = articleDao.updateArticleById(article);
        if (n > 0) {
            return true;
        }
        return false;
    }

    public boolean deleteArticleById(Long id) throws SQLException {
        int n = articleDao.deleteArticleById(id);
        if (n > 0) {
            return true;
        }
        return false;
    }
}
